/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.api.handlers;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import processing.core.PApplet;
import techjit.api.handlers.keyboardactionhandler.KeyboardActionHandler;
import techjit.api.handlers.mouseactionhandler.MouseActionHandler;
import techjit.api.handlers.viewinghandler.ViewingHandler;
import techjit.gfx.core.GFX_Scene;
import techjit.util.RP;
import techjit.util.RuntimeParams;

public class LibraryHandler implements MouseWheelListener {

	// FIELDS
	private PApplet p;
	private GFX_Scene scene1 = new GFX_Scene();

	private ScreenDrawingHandler screenDrawingHandler = new ScreenDrawingHandler();
	private ViewingHandler viewingHandler = new ViewingHandler();

	// METHODS
	public void draw() {
		ParamFileHandler.update(p, scene1);

		BackgroundHandler.draw(p);

		if (RuntimeParams.CAMERA__USE_PEASY_CAM) {
			WorldSystemHandler.draw(p);

			SceneRecursionHandler.draw(p, scene1);

			screenDrawingHandler.draw(p, scene1);
		} else {
			WorldSystemHandler.draw(p);

			p.pushMatrix();
			{
				viewingHandler.setupMatrix(p, scene1);

				SceneRecursionHandler.draw(p, scene1);
			}
			p.popMatrix();

			EdgeDrawingHandler.draw(p, scene1);
		}
	}

	public void init(PApplet p) {
		// api
		this.p = p;
		RegistrationHandler.execute(p, this);

		// viewing
		CameraHandler.setupPeasyCam(p);
		screenDrawingHandler.init(p);

		// params
		ParamFileHandler.init(p);
		AppletHandler.applyParams(p);

		// mapping
		if (RuntimeParams.release == RP.ReleaseEnum.PROTOTYPE) {
			AddSceneElementsHandler.addElements(scene1);
		}
	}

	public void keyEvent(KeyEvent keyEvent) {
		KeyboardActionHandler.processKeyEvent(p, keyEvent, scene1,
				viewingHandler);
	}

	public void mouseEvent(MouseEvent mouseEvent) {
		MouseActionHandler.processMouseEvent(p, mouseEvent, scene1,
				viewingHandler);
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		viewingHandler.processMouseWheelMoved(p, e, scene1);
	}

	public void specifySoftware(String absolutePathToSoftwareRootFolder) {
		if (RuntimeParams.release == RP.ReleaseEnum.RELEASE) {
			if (absolutePathToSoftwareRootFolder.equals("")) {
				RuntimeParams.EXAMPLE__ASSEMBLER_MODE = 2;
			} else {
				RuntimeParams.MODEL__USE_ABSOLUTE_PATH = true;
				RuntimeParams.MODEL__PATH_TO_SOFTWARE_INPUT_ROOT_FOLDER = absolutePathToSoftwareRootFolder;
			}
			AddSceneElementsHandler.addElements(scene1);
		}
	}

	public void specifyViewScreenPosition(int x, int y) {
		// TODO
	}

	public void specifyViewScreenSize(int width, int height) {
		// TODO
	}

}